/*
 *继承修改leaflet原生内容
 */
(function(factory) {
    var L;
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['leaflet'], factory);
    } else if (typeof module === 'object' && typeof module.exports === "object") {
        // Node/CommonJS
        L = require('leaflet');
        module.exports = factory(L);
    } else {
        // Browser globals
        if (typeof window.L === 'undefined')
            throw 'Leaflet must be loaded first';
        factory(window.L);
    }
})(function(L) {

    /*
     *目标透明度设置
     */
    L.Marker.include({
        setOpacity: function(opacity) {
            this.options.opacity = opacity;
            if (this._map) {
                this._updateOpacity();
            }

            return this;
        },

        _updateOpacity: function() {
            var opacity = this.options.opacity;
            if (opacity === 0) {
                this._icon.style.display = "none";
            } else if (opacity === 1) {
                this._icon.style.display = "block";
            } else {
                L.DomUtil.setOpacity(this._icon, opacity);
            }
            if (this._shadow) {
                L.DomUtil.setOpacity(this._shadow, opacity);
            }
        }
    });

    /*
     * 线写文字 canvas   --海洋气象信息
     */
    L.TextCanvas = L.Canvas.extend({

        _updatePoly: function(layer, closed) {
            L.Canvas.prototype._updatePoly.call(this, layer, closed);
            if (layer.options.text) {
                this._text(layer, closed);
            }
        },

        /*
         * 在移除时移除整个canvas，避免遗留多余canvas元素
         * 只针对气象功能
         */
        _removePath: function(layer) {
            layer._removed = true;
            L.DomUtil.remove(this._container);
        },

        _text: function(layer, closed) {
            var ctx = this._ctx,
                text = layer.options.text,
                parts = layer._parts,
                map = this._map;
            if (!parts.length) {
                return;
            }
            try {
                var center = layer.getCenter();
                var pt = map.latLngToLayerPoint(center);
                ctx.textAlign = 'start';
                ctx.textBaseline = 'middle';
                ctx.font = 'Microsoft YaHei';
                ctx.fillStyle = "#000";
                ctx.fillText(text, pt.x, pt.y);
            } catch (ex) {
                var width = this._container.width;
                var height = this._container.height;
                ctx.clearRect(0, 0, width * 2, height * 2);
                // console.log(ex);
                // console.log(this._layers);
            }
        }
    });

    /*
     * 矩形内写文字 canvas
     */
    L.VectorLabelCanvas = L.Canvas.extend({

        initialize: function(options) {
            L.Canvas.prototype.initialize.call(this, options);
            // L.Util.setOptions(this,options);
        },

        _updatePoly: function(layer, closed) {
            L.Canvas.prototype._updatePoly.call(this, layer, closed);
            if (layer.options.text === undefined) {
                return;
            } else {
                this._text(layer, closed);
            }
        },

        _text: function(layer, closed) {
            var ctx = this._ctx,
                text = layer.options.text,
                map = this._map;
            var center = layer.getBounds().getCenter();
            var pt = map.latLngToLayerPoint(center);
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.font = 'Microsoft YaHei';
            ctx.fillStyle = "#000";
            ctx.fillText(text, pt.x, pt.y);
        }
    });

    /*
     * 矩形内写文字 svg
     */
    L.VectorLabelSvg = L.SVG.extend({

        _updatePoly: function(layer, closed) {
            L.SVG.prototype._updatePoly.call(this, layer, closed);
            if (layer.options.text === undefined) {
                return;
            } else {
                this._text(layer, closed);
            }
        },

        _initPath: function(layer) {
            L.SVG.prototype._initPath.call(this, layer);
            if (layer.options.text !== undefined) {
                layer._textele = L.SVG.create('text');
            }
        },

        _addPath: function(layer) {
            L.SVG.prototype._addPath.call(this, layer);
            if (layer._textele) {
                this._rootGroup.appendChild(layer._textele);
            }
        },

        _removePath: function(layer) {
            L.SVG.prototype._removePath.call(this, layer);
            if (layer._textele) {
                L.DomUtil.remove(layer._textele);
            }
        },

        _text: function(layer, closed) {
            var container = this._container,
                text = layer.options.text,
                textele = layer._textele,
                map = this._map;
            var center = layer.getBounds().getCenter();
            var pt = map.latLngToLayerPoint(center);
            textele.setAttribute('x', pt.x);
            textele.setAttribute('y', pt.y + 7);
            textele.setAttribute('text-anchor', 'middle');
            textele.setAttribute('style', 'font-family:"Microsoft YaHei";font-size:14px;');
            textele.innerHTML = text;
        }
    });

    /*
     * 加载错误切片不可见
     */
    L.TileLayer.include({
        _tileOnError: function(done, tile, e) {
            var errorUrl = this.options.errorTileUrl;
            if (errorUrl) {
                tile.src = errorUrl;
            }
            done(e, tile);
            if (!errorUrl) {
                tile.style.visibility = 'hidden';
            }
        }
    });
});
